home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991: Code Warrior / bincue / Code Warrior.bin / Tools & Apps (Moof!) / Testing & Debugging / Report Error 1.1 / stringUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  5.2 KB  |  204 lines  |  [TEXT/KAHL]

  1. /*================================================================================
  2.     stringUtilities.c
  3.     version 1.1
  4.     
  5.     Greg Anderson
  6.     28 June 1991
  7.     greggor@apple.com
  8.     
  9.     Convienient string utility routines.  See the macros defined
  10.     in stringUtilities.h, too.
  11.     
  12. ================================================================================*/
  13. #include "stringUtilities.h"
  14. #include <string.h>
  15.  
  16. #ifndef __TYPES__
  17. #include <Types.h>
  18. #endif
  19.  
  20. /*--------------------------------------------------------------------------------
  21.     Copy a Pascal string into a C string
  22. --------------------------------------------------------------------------------*/
  23. pascal void PtoCcpy( char* cstr, Str255 pstr )
  24. {
  25.     int        i = pstr[0];
  26.     
  27.     cstr[i] = 0;
  28.     while( i > 0 )
  29.         cstr[i - 1] = pstr[i--];
  30. }
  31.  
  32. /*--------------------------------------------------------------------------------
  33.     Copy a C string into a Pascal string
  34. --------------------------------------------------------------------------------*/
  35. pascal void CtoPcpy(  Str255 pstr, char* cstr )
  36. {
  37.     int        i = 1;
  38.     
  39.     while( (*cstr) && (i < 255) )
  40.         pstr[i++] = *cstr++;
  41.     pstr[0] = i - 1;
  42. }
  43.  
  44. /*--------------------------------------------------------------------------------
  45.     Compare a Pascal string with a C string
  46. --------------------------------------------------------------------------------*/
  47. pascal short PtoCcmp( Str255 pstr, char* cstr )
  48. {
  49.     char        tmp[256];
  50.     
  51.     PtoCcpy( tmp, pstr );
  52.     return( strcmp( tmp, cstr ) );
  53. }
  54.  
  55. /*--------------------------------------------------------------------------------
  56.     Copy one pascal string into another
  57. --------------------------------------------------------------------------------*/
  58. pascal void pstrcpy( Str255 dest, Str255 src )
  59. {
  60.     unsigned char*        dp;
  61.     unsigned char*        sp;
  62.     short                i;
  63.     short                max;
  64.     
  65.     sp = (unsigned char*)src;
  66.     dp = (unsigned char*)dest;
  67.     max = *sp;
  68.     
  69.     for( i = 0; i <= max ; ++i )
  70.         *dp++ = *sp++;
  71. }
  72.  
  73. /*--------------------------------------------------------------------------------
  74.     Copy one pascal string onto the end of another
  75. --------------------------------------------------------------------------------*/
  76. pascal void pstrcat( Str255 dest, Str255 src )
  77. {
  78.     unsigned char*        dp;
  79.     unsigned char*        sp;
  80.     unsigned char*        cp;
  81.     short                i;
  82.     short                max;
  83.     
  84.     sp = (unsigned char*)src;
  85.     dp = (unsigned char*)dest;
  86.     max = *sp;
  87.     /*
  88.     // Adjust past length & chars already in dest
  89.     */
  90.     cp = dp + *dp + 1;
  91.     ++sp;
  92.     /*
  93.     // Determine how many characters we can really copy
  94.     */
  95.     if( max + *dp > 255 )
  96.         max = 255 - *dp;
  97.     *dp += max;
  98.     
  99.     for( i = 0; i < max ; ++i )
  100.         *cp++ = *sp++;
  101. }
  102.  
  103. /*--------------------------------------------------------------------------------
  104.     Compare two Pascal strings
  105. --------------------------------------------------------------------------------*/
  106. pascal short pstrcmp( unsigned char* a, unsigned char* b )
  107. {
  108.     short        len = ( *a < *b ? *a : *b );
  109.     short        lenEqual = ( *a > *b ) - ( *a < *b );
  110.     short        equalityTest;
  111.     
  112.     while( len )
  113.     {
  114.         ++a;
  115.         ++b;
  116.         equalityTest = ( *a > *b ) - ( *a < *b );
  117.         
  118.         if( equalityTest )
  119.             return equalityTest;
  120.         
  121.         --len;
  122.     }
  123.     
  124.     return lenEqual;
  125. }
  126.  
  127. /*-------------------------------------------------------------------
  128.     'Partial' string compare - check if s2 appears at the beginning
  129.     of s1.
  130.     
  131.     Identical to 'strncmp(s1,s2,strlen(s2));', with the bonus feature
  132.     of being case-insensitive.
  133. -------------------------------------------------------------------*/
  134. pascal short partialstrcmp( register char* s1, register char* s2)
  135. {
  136.     register char    c1,
  137.                     c2;
  138.     
  139.     while(    c2 = *s2++ )
  140.     {
  141.         c1 = *s1++;
  142.         c1 = ToUpper(c1);
  143.         c2 = ToUpper(c2);
  144.         if( c1 < c2 ) return(-1);
  145.         if( c1 > c2 ) return( 1);
  146.     }
  147.     return(0);
  148. }
  149.  
  150. /*-------------------------------------------------------------------
  151.     Convert an ascii number into a string, advance the string ptr
  152.     past the number
  153. -------------------------------------------------------------------*/
  154. pascal short ScanNumberInString( char** line )
  155. {
  156.     short n = 0;
  157.     while( (**line >= '0') && (**line <= '9') )
  158.     {
  159.         n = n * 10 + (**line - '0');
  160.         ++(*line);
  161.     }
  162.     return n;
  163. }
  164.  
  165. /*-------------------------------------------------------------------
  166.     Convert an ascii number into a string
  167. -------------------------------------------------------------------*/
  168. pascal short NumberInString( char* line )
  169. {
  170.     return( ScanNumberInString( &line ) );
  171. }
  172.  
  173. /*-------------------------------------------------------------------
  174.     Copy characters from **line into the specified pstr
  175. -------------------------------------------------------------------*/
  176. pascal void ScanTextInString( char** line, Str255 pstr, Boolean terminateAtSpace )
  177. {
  178.     short        len = 0;
  179.     
  180.     while( (len < 256) && ( (**line > ' ') || ( (**line == ' ') && (terminateAtSpace == false)) ) )
  181.     {
  182.         ++len;
  183.         pstr[len] = **line;
  184.         (*line)++;
  185.     }
  186.     pstr[0] = len;
  187. }
  188.  
  189. /*-------------------------------------------------------------------
  190.     Copy the next word from **line into the specified pstr
  191. -------------------------------------------------------------------*/
  192. pascal void ScanWordInString( char** line, Str255 pstr )
  193. {
  194.     ScanTextInString( line, pstr, true );
  195. }
  196.  
  197. /*-------------------------------------------------------------------
  198.     Copy the next line from **line into the specified pstr
  199. -------------------------------------------------------------------*/
  200. pascal void ScanLineInString( char** line, Str255 pstr )
  201. {
  202.     ScanTextInString( line, pstr, false );
  203. }
  204.